<?php

namespace App\Logics;

use App\Models\Admin;
use App\Models\Article;
use App\Service\Base\Logic;
use App\Service\HtmlExcel;

class ArticleLogic extends Logic
{

    // 获取数据量
    public function dateCount($dateTime = 0)
    {
        $where = [['verify', '=', 1],];
        if($dateTime){
            $where = [['verify', '=', 1], ['push_time', '>=', $dateTime], ['push_time', '<', $dateTime + 86400]];
        }

        $model = new Article();
        $total = $model->where($where)->count();

        return $total;
    }

    // 获取公告数据量
    public function siteCount($site_id)
    {
        $where = [['verify', '=', 1],];
        if($site_id){
            $where[] = ['site_id', '=', 13];
        }

        $model = new Article();
        $total = $model->where($where)->count();

        return $total;
    }

    // 获取列表数据
    public function onLists()
    {
        $option    = $this->option();
        $page      = $option->getData('page', 1, 'intval');
        $rows      = $option->getData('rows', 10, 'intval');
        $is_page   = $option->getData('is_page', 0, 'intval');
        $site_id   = $option->getData('site_id', 0, 'intval');
        $keyword   = $option->getData('keyword', null, 'trim');
        $sentiment = $option->getData('sentiment', null, 'trim');
        $s_date    = $option->getData('date_start', null, 'trim');
        $e_date    = $option->getData('date_end', null, 'trim');
        if($keyword == '宋正'){
            $keyword = '董事长宋正';
        }

        // 条件
        $where = [['verify', '=', 1],];
        if($site_id){
            $where[] = ['site_id', '=', $site_id];
        }
        if($sentiment){
            $where[] = ['sentiment', '=', $sentiment];
        }
        if($keyword){
            $where[] = ['keyword', '=', $keyword];
        }
        if($s_date){
            $s_time  = strtotime($s_date);
            $where[] = ['push_time', '>=', $s_time];
        }
        if($e_date){
            $e_time  = strtotime($e_date) + 86400;
            $where[] = ['push_time', '<', $e_time];
        } else{
            if($s_date){
                $where[] = ['push_time', '<', $s_time + 86400];
            }
        }

        // 执行查询
        $model  = new Article();
        $result = $model->isCache(true)->listPage($where, $page, $rows, 'push_time DESC');
        $option->assign('lists', $result['list']);

        // 如果是为了制作翻页
        if($is_page){
            return $result;
        }

        return $result['list'];
    }

    // 导出数据
    public function explode()
    {
        $model  = new Article();
        $option = $this->option();
        $s_date = $option->getData('date_start', null, 'trim');
        $e_date = $option->getData('date_end', null, 'trim');

        $channels = $model->getChannels(false);
        $lists    = $this->onLists();
        $xlxTitle = "";
        if($s_date){
            $xlxTitle .= $s_date;
        }
        if($e_date){
            $xlxTitle .= ($xlxTitle ? '-' : '').$e_date;
        }
        $xlxTitle .= "复旦复华舆情文章";

        // 整理
        $result = [];
        foreach($lists as $item){
            $_channel = $item['channel'];
            $_title   = $item['title'] ?: $item['description'];
            $result[] = [
                //'id'        => $item['id'],
                'push_time' => date('Y/m/d', $item['push_time']),
                'from_site' => $item['from_site'],
                'title'     => $_title,
                'from_url'  => $item['from_url'],
                'channel'   => $channels[$_channel] ?? '',
                '分析'        => '',
            ];
        }

        //表头
        $headList = [
            //'id'        => 'id',
            'push_time' => '时间',
            'from_site' => '站点',
            'title'     => '稿件',
            'from_url'  => '链接',
            'channel'   => '类型',
            '分析'        => '分析',
        ];
        array_unshift($result, $headList);


        $headTitle = $xlxTitle;
        $filename  = $xlxTitle.".xls";
        $excel     = new HtmlExcel();
        $headTable = $excel->tableHeader($headTitle, $headList);
        $str       = $excel->excelData($result, $headTable, $filename);

        //exit($str);


        return $str;
    }

}


